# Contributor: Leo <thinkabit.ukim@gmail.com>
# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=linux-tools
pkgver=5.4.35
_kernver=${pkgver%.*}
pkgrel=0
pkgdesc="Linux kernel tools meta package"
url="https://www.kernel.org/"
arch="all"
license="GPL-2.0-only"
depends="perf cpupower $pkgname-gpio $pkgname-iio"
depends_dev="pciutils-dev readline-dev gettext-dev"
makedepends="$depends_dev elfutils-dev bash linux-headers flex bison diffutils
	zlib-dev findutils"
subpackages="perf perf-bash-completion:bashcomp:noarch cpupower
	$pkgname-gpio $pkgname-iio $pkgname-doc $pkgname-dev"
source="https://kernel.org/pub/linux/kernel/v5.x/linux-$_kernver.tar.xz
	https://kernel.org/pub/linux/kernel/v5.x/patch-$pkgver.xz
	cpupower-libs.patch
	disable-Werror.patch
	tools-iio-Correctly-add-make-dependency-for-iio_utils.patch
	"

builddir="$srcdir"/linux-$_kernver

prepare() {
	cd "$srcdir"/linux-$_kernver
	if [ "${pkgver%.0}" = "$pkgver" ]; then
		msg "Applying patch-$pkgver.xz"
		unxz -c < "$srcdir"/patch-$pkgver.xz | patch -p1 -N
	fi
	default_prepare
}

_make_tools() {
	make -C "$builddir"/tools \
		VERSION=$pkgver-$pkgrel NLS=false LIBINTL_LIBS=-lintl WERROR=0 \
		prefix=/usr libdir=/usr/lib lib=lib mandir=/usr/share/man \
		"$@"
}

build() {
	_make_tools perf cpupower gpio iio
}

package() {
	mkdir -p "$pkgdir"
	_make_tools DESTDIR="$pkgdir" \
		perf_install cpupower_install gpio_install iio_install

	mv "$pkgdir"/etc/bash_completion.d "$pkgdir"/usr/share/bash-completion/completions
}

cpupower() {
	pkgdesc="Linux kernel tool to set CPU power features"
	depends=""
	mkdir -p "$subpkgdir"/usr/sbin \
		"$subpkgdir"/usr/bin \
		"$subpkgdir"/usr/lib \
		"$subpkgdir"/etc

	mv "$pkgdir"/usr/bin/cpu* "$subpkgdir"/usr/bin/
	mv "$pkgdir"/usr/sbin/cpu* "$subpkgdir"/usr/sbin/
	mv "$pkgdir"/usr/lib/libcpu*.so.* "$subpkgdir"/usr/lib/
	mv "$pkgdir"/etc/cpu* "$subpkgdir"/etc/
}

perf() {
	pkgdesc="Linux kernel performance auditing tool"
	depends=""
	mkdir -p "$subpkgdir"/usr/bin "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/bin/perf \
		"$pkgdir"/usr/bin/trace \
		"$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/lib/traceevent \
		"$subpkgdir"/usr/lib/
	mv "$pkgdir"/usr/libexec "$subpkgdir"/usr/
}

gpio() {
	pkgdesc="Linux kernel GPIO tools"
	depends=""
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/*gpio* "$subpkgdir"/usr/bin
}

iio() {
	pkgdesc="Linux kernel IIO tools"
	depends=""
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/*iio* "$subpkgdir"/usr/bin
}

bashcomp() {
	replaces="$pkgname-bash-completion" # Backward compatibility
	pkgdesc="Bash autocompletion for $pkgname"
	install_if="perf=$pkgver-r$pkgrel bash-completion"
	mkdir -p "$subpkgdir"/usr/share/
	mv "$pkgdir"/usr/share/bash-completion "$subpkgdir"/usr/share/
}

sha512sums="9f60f77e8ab972b9438ac648bed17551c8491d6585a5e85f694b2eaa4c623fbc61eb18419b2656b6795eac5deec0edaa04547fc6723fbda52256bd7f3486898f  linux-5.4.tar.xz
d697462715c01f40a4c85317f98ffb22d0eac6365041e286ed0d01afedab7285f7183c87cd6eb34971a66f6113d620ae7478bb946268d8f5889b062b7f0d4642  patch-5.4.35.xz
a46e3a84b00a39a356618831d0ddfb7f0d10f0a3799d1307ba2cc832e73c01f8d637a4e801a6dd25025f6f13155c6ad8b836422ff72d365e51063ac0bf907f52  cpupower-libs.patch
a0b90cead5f80a7e785b2fdd8ac9faed1234f4aa5eebf658af8d8b4d3b8593147ff43810d0b28a95b057bb7781113a48723b9cf9432ec8d9510451b51416e3de  disable-Werror.patch
c47b3f9cad4ca427ab0dd59273cdb34c745db57b3afd596338012aefd477f1984071c6615e5a4cce7fdd5229ac4e008fb386d0fc4dcbf79382d4810c14588090  tools-iio-Correctly-add-make-dependency-for-iio_utils.patch"
